package com.hydra.baseui.recyclerlist;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import com.hydra.framework.thread.ThreadBus;

import static com.hydra.framework.thread.ThreadBus.Main;

/**
 * Created by Administrator on 2016/7/12.
 *
 */
public class EndlessHeaderListView extends HeaderListView {

	public EndlessHeaderListView(Context context) {
		super(context);
	}

	public EndlessHeaderListView(Context context, int orientation) {
		super(context, orientation);
	}

	public EndlessHeaderListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public EndlessHeaderListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected RecyclerHeaderListAdapter buildWrappedAdapter(Adapter originAdapter) {
		if (originAdapter != null) {
            return new RecyclerEndlessAdapter(originAdapter);
		}

		return null;
	}

	public void notifyEndlessViewChange() {
        ThreadBus.post(Main, new Runnable() {
            @Override
            public void run() {
                Adapter adapter = getAdapter();
                if (adapter != null) {
                    RecyclerEndlessAdapter endlessAdapter = ((RecyclerEndlessAdapter) adapter);

                    if(endlessAdapter.getItemCount() > 0) {
						endlessAdapter.notifyEndless();
                    }
                }
            }
        });
	}

	/**
	 * after setAdapter
	 */
	public void setEndlessView(View view) {
		Adapter adapter = getAdapter();
		if (adapter != null) {
			((RecyclerEndlessAdapter) adapter).setEndlessView(view);
		}
	}

	/**
	 * after setAdapter
	 */
	public void removeEndlessView() {
		Adapter adapter = getAdapter();
		if (adapter != null) {
			((RecyclerEndlessAdapter) getAdapter()).removeEndlessView();
		}
	}

	/**
	 * after setAdapter
	 */
	public void setEndlessListener(EndlessListener endlessListener) {
		Adapter adapter = getAdapter();
		if (adapter != null) {
            ((RecyclerEndlessAdapter) adapter).setEndlessListener(endlessListener);

            notifyEndlessViewChange();
		}
	}

    public View getEndlessView() {
        Adapter adapter = getAdapter();
        if (adapter != null) {
            return ((RecyclerEndlessAdapter) getAdapter()).getEndlessView();
        }

        return null;
    }
}
